package org.huawei260.shousi;

import java.util.Scanner;
import java.util.Stack;
public class OD_2_行星碰撞 {

//	public class Solution {
		public static int[] asteroidCollision(int[] planets) {
			Stack<Integer> stack = new Stack<>();
			for (int planet : planets) {
				boolean exploded = false;
				while (!stack.isEmpty() && planet < 0 && stack.peek() > 0) {
					if (stack.peek() < -planet) {
						stack.pop();
						continue;
					} else if (stack.peek() == -planet) {
						stack.pop();
					}
					exploded = true;
					break;
				}
				if (!exploded) {
					stack.push(planet);
				}
			}

			int[] result = new int[stack.size()];
			for (int i = stack.size() - 1; i >= 0; i--) {
				result[i] = stack.pop();
			}
			return result;
		}

	public static void main(String[] args) {

			int[] n = new int[]{-5, -10, 5};
		for (int i : asteroidCollision(n)) {
			System.out.print(i + " ");


		}

		Scanner sc = new Scanner(System.in);


//		System.out.println(asteroidCollision(n).toString());

	}

}
